package com.bike.bikeserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bike.bikeserver.common.core.domain.CommonResult;
import com.bike.bikeserver.common.utils.StringUtils;
import com.bike.bikeserver.domain.Bike;
import com.bike.bikeserver.mapper.BikeMapper;
import com.bike.bikeserver.service.IBikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/7/9 21:46
 */
@Service
public class BikeServiceImpl implements IBikeService
{
    @Autowired
    BikeMapper bikeMapper;

    @Override
    public CommonResult getAllBikeNames()
    {
        List<Bike> bikes = bikeMapper.selectList(null);
        List<String> bikeNames = new ArrayList<>();
        for (Bike bike : bikes)
        {
            bikeNames.add(bike.getBikeName());
        }
        return CommonResult.success(bikeNames);
    }

    @Override
    public CommonResult getBikeList()
    {
        List<Bike> bikes = bikeMapper.selectList(null);
        return CommonResult.success(bikes);
    }

    @Override
    public CommonResult getBikeLikeBikeName(String bikeName)
    {
        LambdaQueryWrapper<Bike> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Bike::getBikeName, bikeName);
        List<Bike> bikes = bikeMapper.selectList(wrapper);
        return CommonResult.success("查询成功！为您找到" + bikes.size() + "条记录！", bikes);
    }

    @Override
    public CommonResult editBikeById(Bike bike)
    {
        Bike bike1 = bikeMapper.selectById(bike.getId());
        if (bike1.getBikeName().equals(bike.getBikeName()) && bike1.getBelongingCompany().equals(bike.getBelongingCompany()))
        {
            return CommonResult.warn("请勿输入空值或重复修改！");
        }
        bike1.setBikeName(bike.getBikeName());
        bike1.setBelongingCompany(bike.getBelongingCompany());
        int update = bikeMapper.updateById(bike1);
        if (update != 0)
        {
            return CommonResult.success("修改成功！");
        }
        return CommonResult.error();
    }

    @Override
    public CommonResult deleteBikeById(Bike bike)
    {
        int count = bikeMapper.deleteById(bike.getId());
        if (count != 0)
        {
            return CommonResult.success("已成功删除！");
        }
        return CommonResult.error();
    }

    @Override
    public CommonResult addBike(Bike bike)
    {
        if (bike.getBikeName() == null || StringUtils.isBlank(bike.getBikeName()) || bike.getBelongingCompany() == null || StringUtils.isBlank(bike.getBelongingCompany()))
        {
            return CommonResult.warn("输入的属性不可有空值！");
        }
        bike.setCreateTime(new Date());
        bike.setUpdateTime(new Date());
        int insert = bikeMapper.insert(bike);
        if (insert != 0)
        {
            return CommonResult.success("添加成功！");
        }
        return CommonResult.error();
    }
}